home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus Leser 19 / Amiga Plus Leser CD 19.iso / Tools / Freeware / Swf_Player / Player / main.c < prev    next >
Encoding:
C/C++ Source or Header  |  2002-11-17  |  10.3 KB  |  447 lines

  1. /*
  2. **
  3. ** $VER: main.c 0.3 (05.11.02)
  4. ** Description
  5. **    Main program
  6. **
  7. ** (C) Copyright 1999 Paul Hill
  8. ** (C) Copyright 2002 Alexandre Balaban
  9. **
  10. ** $HISTORY :
  11. **        - 0.3, 05.11.02 : Added pack pragma directives for PPC compilation
  12. **        - 0.2, 03.10.02 : o Include PPC compilation improvements by Steffen Haeuser
  13. **                        o Added LOOP settings
  14. **      - 0.1, 08.03.99 : Original version by Paul Hill
  15. **
  16. */
  17.  
  18.  
  19.  
  20. #include <stdio.h>
  21.  
  22. #pragma pack(2)
  23.  
  24. #ifndef __PPC__
  25. #include <proto/dos.h>
  26. #include <proto/exec.h>
  27. #include <proto/icon.h>
  28. #include <proto/intuition.h>
  29. #include <proto/asl.h>
  30. #include <proto/graphics.h>
  31. #else
  32. #include </ADE/os-includeppc/proto/dos.h>
  33. #include </ADE/os-includeppc/proto/exec.h>
  34. //#include </ADE/os-includeppc/proto/icon.h>
  35. #include </ADE/os-includeppc/proto/intuition.h>
  36. #include </ADE/os-includeppc/proto/asl.h>
  37. #include </ADE/os-includeppc/proto/graphics.h>
  38. #endif // __PPC__
  39. #include <graphics/gfxbase.h>
  40. #include <intuition/intuition.h>
  41. #include <workbench/startup.h>
  42.  
  43. #pragma pack()
  44.  
  45. #include <string.h> // [SHA, 03/10/2002 : missing include ?]
  46.  
  47. #include "flash.h"
  48. #include "swfplayer.h"
  49.  
  50. #include "timer.h"
  51.  
  52. #define TEMPLATE "FILE,PUBSCREEN/K,INFO/S,MONOSOUND/S,MAXWIDTH/K/N,MAXHEIGHT/K/N,ACTIVEPLAY/S"
  53.  
  54. unsigned long __stack = 12288; // ABA: Added some more stack
  55.  
  56. static const char *version = "$VER: SWFPlayer 1.2c ("__DATE__")";
  57.  
  58. void __chkabort(void){}
  59. char __stdiowin[]="con:0/0//200/SWFPlayer Output/close/wait";
  60.  
  61. struct Library *CyberGfxBase=NULL;
  62.  
  63. #ifdef __PPC__
  64. struct GfxBase *GfxBase = NULL;
  65. struct IntuitionBase *IntuitionBase = NULL;
  66. struct Library *AslBase = NULL;
  67. struct Library *GadToolsBase = NULL;
  68. #else
  69. extern struct GfxBase *GfxBase;
  70. #endif // __PPC__
  71.  
  72. LONG args[OPT_COUNT];
  73.  
  74. struct _swfinfo swfinfo;
  75. static char filename[MAXPATH];
  76.  
  77. /* Functions definitions (See each function for description) */
  78. void    drawinfo();
  79. LONG    open_swf_file( char * fname );
  80. ULONG    playswf(char *fname);
  81. ULONG    getfilename( char * name, char * title );
  82. long    AmigaFlashGraphicInit( FlashHandle fh );
  83.  
  84.  
  85. /*
  86. ** DrawInfo()
  87. **    - prints out misc flash informations
  88. */
  89. void drawinfo()
  90. {
  91.  printf("%s (Flash %ld)  - Frames = %ld  - Rate = %ld fps\n", swfinfo.filename,swfinfo.fi.version,swfinfo.fi.frameCount,swfinfo.fi.frameRate);
  92. }
  93.  
  94.  
  95. /*
  96. ** open_swf_file( char * fname )
  97. ** - reads all data from specified file into buffer
  98. */
  99. LONG open_swf_file( char * fname )
  100. {
  101.   struct FileInfoBlock *MyInfoBlock;
  102.   LONG rc = 1;
  103.   BPTR MyLock, fh;
  104.   LONG bytesread;
  105.  
  106.   if (!fname) return 1;
  107.  
  108.   TRACE("Entering open_swf_file...\n");
  109.  
  110.   MyInfoBlock = (struct FileInfoBlock*)AllocDosObject (DOS_FIB, TAG_END);
  111.   if (MyInfoBlock)
  112.   {
  113.     MyLock = Lock (fname, ACCESS_READ);
  114.     if (MyLock)
  115.     {
  116.       if (Examine (MyLock, MyInfoBlock))
  117.       {
  118.         if (MyInfoBlock->fib_DirEntryType < 0)
  119.         {
  120.           fh = OpenFromLock(MyLock);
  121.           if (fh)
  122.           {
  123.             swfinfo.buffer = (char *) AllocVec(MyInfoBlock->fib_Size, MEMF_ANY);
  124.             if (swfinfo.buffer)
  125.             {
  126.               bytesread = Read(fh,swfinfo.buffer,MyInfoBlock->fib_Size);
  127.               if (bytesread == MyInfoBlock->fib_Size)
  128.               {
  129.                 swfinfo.filename = fname;
  130.                 swfinfo.filesize = MyInfoBlock->fib_Size;
  131.                 rc = 0;
  132.               }
  133.             }
  134.             Close(fh);
  135.             TRACE("fh closed\n");
  136.           }
  137.           else
  138.           {
  139.             /* OpenFromLock() failed, so release the lock */
  140.             UnLock (MyLock);
  141.           }
  142.         } else
  143.         {
  144.           /* We can't open directories */
  145.           SetIoErr(ERROR_OBJECT_WRONG_TYPE);
  146.         }
  147.       }
  148.     }
  149.  
  150.     FreeDosObject (DOS_FIB, MyInfoBlock);
  151.   }
  152.  
  153.   TRACE("Exiting open_swf_file with rc %ld\n", rc );
  154.   return rc;
  155. }
  156.  
  157.  
  158. /*
  159. ** getfilename()
  160. ** - ask the user for a file name using the ASL file requester
  161. */
  162. ULONG getfilename( char * name, char * title)
  163. {
  164.   struct FileRequester *fr;
  165.   char dirname[MAXPATH];
  166.   char *filename;
  167.   ULONG rc=0;
  168.  
  169.   dirname[0] = NULL;
  170.   filename = dirname;
  171.  
  172.   if( ( fr = (struct FileRequester *)
  173.     AllocAslRequestTags(    ASL_FileRequest,
  174.     ASLFR_TitleText,            (ULONG) title,
  175.     ASLFR_InitialFile,        (ULONG) filename,
  176.     ASLFR_InitialDrawer,        (ULONG) dirname,
  177.     ASLFR_InitialPattern,    (LONG) "#?.swf",
  178.     ASLFR_Screen,                 (LONG) swfinfo.screen,
  179.     ASLFR_DoPatterns,            TRUE,
  180.     TAG_DONE) ) )
  181.   {
  182.     if (AslRequest(fr, NULL))
  183.     {
  184.       strcpy(name,fr->rf_Dir);
  185.       AddPart(name,fr->rf_File,MAXPATH);
  186.       rc = 1;
  187.     }
  188.     FreeAslRequest(fr);
  189.   }
  190.   return rc;
  191. }
  192.  
  193. /*
  194. **
  195. ** AmigaFlashInitGraphic( FlashHandle ) - Amiga specific FlashInit
  196. **
  197. */
  198. long AmigaFlashGraphicInit( FlashHandle fh )
  199. {
  200.   long bpp = 0, bpl = 0, pad = 4;
  201.  
  202.   if( NULL != swfinfo.rport )
  203.   {
  204.     switch( GetBitMapAttr(swfinfo.rport->BitMap,BMA_DEPTH) )
  205.     {
  206.     case 1:
  207.     case 2:
  208.     case 3:
  209.     case 4:
  210.     case 5:
  211.     case 6:
  212.     case 7:
  213.     case 8:
  214.       bpp = 1;
  215.       pad = 2;
  216.       break;
  217.  
  218.     case 15:
  219.     case 16:
  220.       bpp = 4; /* We don't support bpp=2 (16 bits/pixel) yet, so use 32 bits/pixel */
  221.       break;
  222.  
  223.     case 24:
  224.     case 32:
  225.       bpp = 4;
  226.       break;
  227.  
  228.     default:
  229.       fprintf(stderr,"Warning: Unsupported screen depth\n");
  230.       bpp = 1;
  231.       pad = 2;
  232.     }
  233.  
  234.     if (bpp) {
  235.       bpl = (swfinfo.width*bpp + pad-1)/pad*pad;
  236.     } else {
  237.       bpl = (swfinfo.width/8 + pad-1)/pad*pad;
  238.     }
  239.  
  240.     TRACE("*** Infos bpp = %ld, bpl = %ld, pad = %ld, AllocVec(%ld)\n",
  241.                                     bpp, bpl, pad, bpl * swfinfo.height );
  242.  
  243.     swfinfo.fd = new FlashDisplay;
  244.  
  245.     swfinfo.fd->pixels = (char*) AllocVec(bpl * swfinfo.height, MEMF_ANY);
  246.     swfinfo.fd->width    = swfinfo.width;
  247.     swfinfo.fd->height= swfinfo.height;
  248.     swfinfo.fd->bpl    = bpl;
  249.     swfinfo.fd->depth    = swfinfo.depth;
  250.     swfinfo.fd->bpp    = bpp;
  251.  
  252.     return FlashGraphicInit( fh, swfinfo.fd );
  253.   }
  254.   else
  255.   {
  256.     PrintFault( 1, "Rastport not initialized !" );
  257.     return 0;
  258.   }
  259. }
  260.  
  261. /*
  262. **
  263. ** playswf(name) - plays the specified swf file (if it exists).
  264. **
  265. */
  266. ULONG playswf(char *fname)
  267. {
  268.   FlashHandle flashHandle = FlashNew();
  269.   ULONG rc = 0;
  270.  
  271.   if( open_swf_file(fname) == 0 )
  272.   {
  273.     int level = 0;
  274.     int status = FlashParse(flashHandle, level, swfinfo.buffer, swfinfo.filesize);
  275.  
  276.     TRACE("FlashParse returned with status = %d\n", status );
  277.  
  278.     if( status & FLASH_PARSE_START )
  279.     {
  280.       FlashGetInfo(flashHandle, &swfinfo.fi);
  281.       TRACE( "*** FlashGetInfo passed !\n" );
  282.  
  283.       /* width & height will hold the innner dimensions for our window */
  284.       swfinfo.width  = swfinfo.fi.frameWidth/20;
  285.       swfinfo.height = swfinfo.fi.frameHeight/20;
  286.  
  287.       /* width & height MUST be even, or swfplayer crashes.  Don't know why
  288.       yet, so I do a simple fix here */
  289.       if (swfinfo.width  & 1) swfinfo.width++;
  290.       if (swfinfo.height & 1) swfinfo.height++;
  291.  
  292.       if (args[OPT_INFO]) drawinfo();
  293.  
  294.       if (open_swf_window()) // open_swf_window() can modify swfinfo.width & height!!!
  295.       {
  296.         int audioflags=0;
  297.         TRACE( "*** open_swf_window() passed !\n" );
  298.  
  299.         AmigaFlashGraphicInit( flashHandle );
  300.         TRACE( "*** AmigaFlashGraphicInit passed !\n" );
  301.  
  302.         if (args[OPT_MONO]) audioflags |= AMIGAFLAG_MONO;
  303.  
  304.         /* It does not work yet :-(
  305.         AmigaFlashSoundInit(flashHandle, audioflags);
  306.         TRACE( "*** AmigaFlashSoundInit passed\n" );
  307.         */
  308.  
  309.         // [ABA, 02/10/2002 : add LOOP settings]
  310.         FlashSettings(flashHandle, PLAYER_LOOP);
  311.         TRACE( "*** FlashSettings passed\n" );
  312.         // [END ABA, 02/10/2002]
  313.  
  314.         FlashSetGetUrlMethod(flashHandle, showUrl, 0);
  315.         TRACE( "*** FlashSetGetUrlMethod passed\n" );
  316.  
  317.         rc = playMovie(flashHandle, swfinfo.screen, swfinfo.window);
  318.         TRACE( "*** playMovie returned code %ld\n", rc );
  319.  
  320.         FlashClose(flashHandle);
  321.         TRACE( "*** FlashClose passed\n" );
  322.  
  323.         close_swf_window();
  324.         TRACE( "*** swf_window closed\n" );
  325.       }
  326.       else
  327.         PrintFault(15,"open_swf_window failed");
  328.     }
  329.     else
  330.       PrintFault(16,"FlashParse error");
  331.  
  332.     if( swfinfo.buffer )
  333.     {
  334.       FreeVec(swfinfo.buffer);
  335.       swfinfo.buffer = NULL;
  336.     }
  337.  
  338.     if( swfinfo.fd )
  339.     {
  340.       if( swfinfo.fd->pixels )
  341.       {
  342.         FreeVec( swfinfo.fd->pixels );
  343.         swfinfo.fd->pixels = NULL;
  344.       }
  345.       delete swfinfo.fd;
  346.     }
  347.   }
  348.   else
  349.   {
  350.     PrintFault(IoErr(),swfinfo.filename);
  351.   }
  352.  
  353.   return rc;
  354. }
  355.  
  356.  
  357.  
  358. int main(void)
  359. {
  360.   struct RDArgs *rd,*myrd=NULL;
  361.   int err=ERROR_REQUIRED_ARG_MISSING;
  362.   char *fname = (char *) NULL;
  363.  
  364. // [SHA, 03/10/2002 : improved PPC compilation]
  365. #ifdef __PPC__
  366.   GfxBase = (struct GfxBase *)OpenLibrary("graphics.library",0);
  367.   IntuitionBase = (struct IntuitionBase *)OpenLibrary("intuition.library",0);
  368.   AslBase = OpenLibrary("asl.library",0);
  369.   GadToolsBase = OpenLibrary("gadtools.library",0);
  370. #endif // __PPC__
  371. // [END SHA, 03/10/2002]
  372.  
  373.   memset( &swfinfo, NULL, sizeof(swfinfo) ); // [SHA, 03/10/2002 : potential problem]
  374.  
  375.   swfinfo.task = FindTask(NULL);
  376.   swfinfo.oldtaskpri = SetTaskPri(swfinfo.task,0);
  377.   SetTaskPri(swfinfo.task,swfinfo.oldtaskpri);
  378.  
  379.   TRACE( "*** GfxBase version = %d\n", GfxBase->LibNode.lib_Version );
  380.   if (GfxBase->LibNode.lib_Version >= 39)
  381.     swfinfo.kick31 = 1;
  382.   else
  383.     swfinfo.kick31 = 0;
  384.  
  385.   /* Attempt to open cgfx.  If it doesn't open, then that's OK */
  386.   CyberGfxBase = OpenLibrary("cybergraphics.library", 40L);
  387.  
  388.   filename[0] = NULL;
  389.  
  390.   rd=ReadArgs(TEMPLATE,args,NULL);
  391.   if (rd)
  392.   {
  393.     if (args[OPT_PUBSCREEN]) swfinfo.pubscreenname = (UBYTE *) args[OPT_PUBSCREEN];
  394.  
  395.     /* Open the timer.device */
  396.     if (opentimer())
  397.     {
  398.       TRACE("timer opened\n");
  399.       err = 0;
  400.       if (args[OPT_FILE])
  401.       {
  402.         /* Filename given on the command line */
  403.         fname = (char *) args[OPT_FILE];
  404.         TRACE("fname = '%s'\n", fname );
  405.       }
  406.       else
  407.       {
  408.         /* No filename given, so pop up an ASL file requester */
  409.         if (getfilename(filename,"Please select a file"))
  410.         {
  411.           fname = filename;
  412.         }
  413.         else
  414.         {
  415.           fname = 0;
  416.         }
  417.       }
  418.  
  419.       if (fname) playswf(fname);
  420.  
  421.       closetimer();
  422.       TRACE("*** timer closed\n");
  423.     }
  424.  
  425.     FreeArgs(rd);
  426.     TRACE( "*** Args freed\n" );
  427.  
  428.     if( myrd ) FreeDosObject(DOS_RDARGS,myrd);
  429.   }
  430.   else
  431.   {
  432.     err=IoErr();
  433.   }
  434.  
  435.   if (err)
  436.   {
  437.     PrintFault(err,"SWFPlayer failed");
  438.   }
  439.   SetIoErr(err);
  440.  
  441.   if (CyberGfxBase) CloseLibrary(CyberGfxBase);
  442.   TRACE( "*** CGX library closed\n" );
  443.  
  444.   TRACE( "*** Main returning %d\n", err );
  445.   return err;
  446. }
  447.